import { Component, Input, OnInit, TemplateRef } from '@angular/core';
import { Device }                                from "@shared/model/device-dto";
import { Observable }                            from "rxjs";
import { Store }                                 from "@shared/model/store-dto";
import { StoreService }                          from "../../../manager/store/store.service";
import { NzMessageService, NzModalService }      from "ng-zorro-antd";
import { copy }                                  from '@delon/util';
import { DeviceService }                         from "../../device.service";
import { deviceStatus }                          from "../../index";

@Component({
    selector   : 'app-device-view-info',
    templateUrl: './info.component.html',
})
export class DeviceViewInfoComponent implements OnInit
{
    
    @Input()
    deviceId: string;
    
    device: Device;
    store$: Observable<Store>;
    stores: Store[];
    status = deviceStatus;

    constructor( private service: DeviceService,
                 private storeSrv: StoreService,
                 public msgSrv: NzMessageService,
                 private modal: NzModalService )
    { }
    
    ngOnInit()
    {
        if ( this.deviceId )
        {
            this.service.load(this.deviceId).subscribe(value =>
            {
                this.device = value;
                if ( value.storeId )
                    this.store$ = this.storeSrv.load(value.storeId);
            })
        }
    }
    
    copy( content: string )
    {
        copy(content).then(value => this.msgSrv.info('复制成功'));
    }
    
    editStore( tp: TemplateRef<any> )
    {
        this.storeSrv.loadAll().subscribe(value =>
        {
            this.stores = value;
            this.modal.create({
                nzTitle  : "修改商家",
                nzContent: tp,
                nzOnOk   : () => this.service.update(this.device).subscribe()
            }).afterClose.subscribe(res =>
            {
                if ( res )
                    this.store$ = this.storeSrv.load(this.device.storeId);
            })
        })
        
    }
}
